//
//  CVOrderButton.m
//  eConsume
//
//  Created by haoyi on 14-3-10.
//  Copyright (c) 2014年 Hooy1. All rights reserved.
//

#import "CVOrderButton.h"

@implementation CVOrderButton

static float const g_fWidth_Order = 8;
static float const g_fHeight_Order = 5;
static float const g_fDistance = 5;  //标题与图标的间距

@synthesize titleLabel = _titleLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [self addSubview:_titleLabel];
        
        _statusImageView = [[UIImageView alloc] init];
        [self addSubview:_statusImageView];
    }
    return self;
}

-(void)setTitle:(NSString *)title forState:(UIControlState)state{
    
    _titleLabel.text = title;
    
    CGSize titleSize = [self sizeForString:title
                                      font:_titleLabel.font
                         constrainedToSize:CGSizeMake(self.frame.size.width - g_fWidth_Order - g_fDistance,
                                                      self.frame.size.height)
                             lineBreakMode:self.titleLabel.lineBreakMode];
    
    _titleLabel.frame = CGRectMake((self.frame.size.width - titleSize.width - g_fWidth_Order - g_fDistance)/2,
                                   0, titleSize.width, self.frame.size.height);
    _statusImageView.frame = CGRectMake(_titleLabel.frame.origin.x + _titleLabel.frame.size.width + g_fDistance,
                                        (_titleLabel.frame.size.height - g_fHeight_Order)/2,
                                        g_fWidth_Order, g_fHeight_Order);
}

-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state{

    _titleLabel.textColor = color;
}

-(void)setImage:(UIImage *)image forState:(UIControlState)state{
    
    if (state == UIControlStateNormal) {
        
        _unselectedImage = image;
        _statusImageView.image = _unselectedImage;
    }else if (state == UIControlStateSelected){
        
        _selectedImage = image;
    }
}

-(void)setSelected:(BOOL)selected{

    if (selected){
        
        _statusImageView.image = _selectedImage;
    }else{
        
        _statusImageView.image = _unselectedImage;
    }
    
    [super setSelected:selected];
}

-(void)setSelected:(BOOL)selected withOrdered:(BOOL)isOrdered{
    
    [self setOrdered:isOrdered];
    if (selected){
        
        _titleLabel.textColor = [self titleColorForState:UIControlStateSelected];
        
        if (isOrdered)
            
            _statusImageView.image = _selectedImage;
        else
            _statusImageView.image = self.orderImage;
    }else{
        
        _titleLabel.textColor = [self titleColorForState:UIControlStateNormal];
        _statusImageView.image = _unselectedImage;
    }
    
    [super setSelected:selected];
}

- (CGSize)sizeForString:(NSString *)string
                   font:(UIFont *)font
      constrainedToSize:(CGSize)constrainedSize
          lineBreakMode:(NSLineBreakMode)lineBreakMode {
    
    if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = lineBreakMode;
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle};
        CGRect boundingRect = [string boundingRectWithSize:constrainedSize
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                attributes:attributes
                                                   context:nil];
        
        return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
    }
    
    return [string sizeWithFont:font constrainedToSize:constrainedSize lineBreakMode:lineBreakMode];
}

@end
